﻿using LSQL;
//using S_chart;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Shopfloormanagement
{
    public partial class Form_GRinputForm : Form
    {
        public string type = "";
        public int RedGreen = 0;
        public int Day = 0;
        public int FactoryID;
        public int DepartmentID;
        public int Level;
        public string ControlType;
        public int ControlOrder;
        public string KPItype;
        public int Year;
        public int Month;
        public DataGridView dgv;
        public Form_GRinputForm()
        {
            InitializeComponent();
        }
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btn_Save_Click(object sender, EventArgs e)
        {
            string Condition = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and Level=" + Level + "and ControlType='" + ControlType + "'and ControlOrder=" + ControlOrder + "and KPItype='" + KPItype + "' and Year=" + Year + " and Month=" + Month;
            if (RedGreen > 0)
            {
                if (SQL.Update(SystemInfo.SQL, "Data_DataGridView", "[" + Day + "]=" + RedGreen, Condition) == true)
                {
                    Sunny.UI.UIMessageTip.Show("成功！", Sunny.UI.TipStyle.Green);
                    Form_Department.department.LoadData_DGV1(ControlType, ControlOrder, KPItype, dgv, 1);
                    dgv.CurrentCell = dgv.Rows[0].Cells[0];

                }
                string Condition2 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and Level=" + Level + "and ControlType='文本表格'and ControlOrder=" + (ControlOrder + 1) + "and KPItype='" + KPItype + "' and Year=" + Year + " and Month=" + Month;
                DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition2);
                if (dt1.Rows.Count > 0)
                {
                    if (RedGreen == 2||RedGreen == 1)
                    {
                        if (txt_Comment.Text != "")
                        {
                            string comment = $"[{Day}] = '{txt_Comment.Text}'";
                            SQL.Update(SystemInfo.SQL, "Data_DataGridView", comment, Condition2);
                            Form_Department.department.LoadData_DGV4("文本表格", (ControlOrder + 1), KPItype, (DataGridView)Form_Department.department.panel1.Controls[KPItype + "_文本表格_" + (ControlOrder + 1)], 1);
                        }
                        else
                        {
                            SQL.Update(SystemInfo.SQL, "Data_DataGridView", "[" + Day + "]=" + "''", Condition2);
                            Form_Department.department.LoadData_DGV4("文本表格", (ControlOrder + 1), KPItype, (DataGridView)Form_Department.department.panel1.Controls[KPItype + "_文本表格_" + (ControlOrder + 1)], 1);
                        }
                    }
                    else
                    {
                        SQL.Update(SystemInfo.SQL, "Data_DataGridView", "[" + Day + "]=" + "''", Condition2);
                        Form_Department.department.LoadData_DGV4("文本表格", (ControlOrder + 1), KPItype, (DataGridView)Form_Department.department.panel1.Controls[KPItype + "_文本表格_" + (ControlOrder + 1)], 1);
                    }
                }
                //2024.11.07增加打开红绿表格显示附件文本的          
                string Condition3 = "Year=" + Year + " and Month=" + Month + " and FactoryID=" + SystemInfo.readFactory + " and DepartmentID =" + DepartmentID + " and ControlOrder=" + ControlOrder + " and KPItype='" + KPItype + "' and ControlType='" + ControlType + "_comment'";
                DataTable dataTable = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition3);
                if (dataTable.Rows.Count > 0)
                {
                    SQL.Update(SystemInfo.SQL, "Data_DataGridView", "[" + Day + "]='" + txt_Comment.Text + "'", Condition3);
                }
                this.Close();
            }
        }
        public void chk_Green_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Green.Checked)
            {
                chk_Red.Checked = false;
                chk_Gray.Checked = false;
                RedGreen = 1;
            }
            else
            {
                RedGreen = 0;
            }
        }
        private void chk_Red_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Red.Checked)
            {
                chk_Green.Checked = false;
                chk_Gray.Checked = false;
                RedGreen = 2;
            }
            else
            {
                RedGreen = 0;
            }
        }
        private void chk_Gray_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Gray.Checked)
            {
                chk_Green.Checked = false;
                chk_Red.Checked = false;
                RedGreen = 3;
            }
            else
            {
                RedGreen = 0;
            }
        }
        private void GRinputForm1_Shown(object sender, EventArgs e)
        {
            if (RedGreen == 1)
            {
                chk_Green.Checked = true;
            }
            else if (RedGreen == 2)
            {
                chk_Red.Checked = true;
            }
            else if (RedGreen == 3)
            {
                chk_Gray.Checked = true;
            }
        }
    }
}